package org.zjx.app.action.window;

import org.zjx.analysis.analyze.window.impl.DialogueAnalyzer;
import org.zjx.app.action.AbstractWindowAction;
import org.zjx.command.entity.ParagraphBean;
import org.zjx.window.operation.WindowOperation;
import org.zjx.window.window.IWindow;

import java.awt.*;
import java.util.List;

public class DialogueAction extends AbstractWindowAction {
    protected DialogueAnalyzer dialogueAnalyzer;
    protected final List<String> dialogueList;

    public DialogueAction(IWindow targetWindow, List<String> dialogueList) {
        super(targetWindow, true);
        this.dialogueList = dialogueList;
        analysis();
    }

    private void analysis() {
        dialogueAnalyzer = new DialogueAnalyzer(targetWindow);
    }

    @Override
    protected void doExecute() throws Exception {
        mouseAloofWindow(dialogueAnalyzer.windowArea == null? dialogueAnalyzer.getOriginBounds(): dialogueAnalyzer.windowArea);
        if (dialogueList == null || dialogueList.isEmpty()) {
            close();
            return;
        }

        int currentButtonIndex = 0;
        int maxAttempts = 15; // 防止无限循环


        while (dialogueAnalyzer.isOpened() && currentButtonIndex < dialogueList.size() && maxAttempts-- > 0) {
            targetWindow.flushScreenshot();
            analysis();
            List<ParagraphBean.Word> buttons = dialogueAnalyzer.getWordList();
            if (buttons == null || buttons.isEmpty()) {
                //点击中间跳页
                click(dialogueAnalyzer.conversion(dialogueAnalyzer.windowArea, dialogueAnalyzer.getContentArea()));
                continue;
            }

            // 检查是否有后续按钮能匹配（包括当前按钮）
            int foundIndex = findNextMatchIndex(currentButtonIndex, buttons);
            if (foundIndex == -1) {
                click(dialogueAnalyzer.conversion(dialogueAnalyzer.windowArea, dialogueAnalyzer.getContentArea()));
                continue;
            }

            // 如果找到的是后续按钮（说明当前按钮缺失）
            if (foundIndex > currentButtonIndex) {
                currentButtonIndex = foundIndex; // 直接跳到找到的按钮
            }

            // 点击找到的按钮
            ParagraphBean.Word word = buttons.stream()
                    .filter(w -> isMatch(dialogueList.get(foundIndex), w.getName()))
                    .findFirst().orElse(null);
            if (word != null) {
                Rectangle buttonRectangle = dialogueAnalyzer.conversion(dialogueAnalyzer.windowArea,dialogueAnalyzer.getContentArea());
                buttonRectangle = dialogueAnalyzer.conversion(buttonRectangle,word.getRectangle());
                click(buttonRectangle);
                currentButtonIndex++;
            }
        }
        targetWindow.flushScreenshot();
        analysis();
        if (dialogueAnalyzer.isOpened()){
            close();
        }
    }

    private int findNextMatchIndex(int startIndex, List<ParagraphBean.Word> buttons) {
        for (int i = startIndex; i < dialogueList.size(); i++) {
            String target = normalizeText(dialogueList.get(i));
            for (ParagraphBean.Word word : buttons) {
                if (isMatch(target, word.getName())) {
                    return i; // 返回第一个匹配的按钮索引
                }
            }
        }
        return -1;
    }

    private boolean isMatch(String target, String buttonText) {
        return isOrderedContains(normalizeText(target), normalizeText(buttonText));
    }

    private String normalizeText(String text) {
        return text.replace(",", "").replace("，", "").trim();
    }

    private void click(Rectangle contentRect) {
        targetWindow.actions().gameMoveTo(contentRect).delay(30).click(1).delay(500);
    }

    /**
     * 检查 s1 是否按顺序包含 s2 的所有字符（不要求连续）
     * @param s1 源字符串
     * @param s2 目标字符串
     * @return true - s1 按顺序包含 s2 的所有字符；false - 不包含或顺序不一致
     */
    private boolean isOrderedContains(String s1, String s2) {
        // 处理边界情况
        if (s2 == null || s2.isEmpty()) {
            return true; // 空字符串总是被包含
        }
        if (s1 == null || s1.isEmpty()) {
            return false; // s1为空但s2非空时，不包含
        }

        int index = 0; // 当前在s1中的查找起始位置
        for (char c : s2.toCharArray()) {
            // 从当前index开始查找字符c
            int pos = s1.indexOf(c, index);
            if (pos == -1) {
                return false; // 未找到字符
            }
            index = pos + 1; // 更新查找起始位置为找到位置的下一位
        }
        return true; // 所有字符均按顺序找到
    }

    @Override
    public void open() {

    }

    @Override
    public void close() {
        if (!dialogueAnalyzer.isOpened()) return;
        Rectangle closeButtonPoint = dialogueAnalyzer.getCloseButtonPoint();
        targetWindow.clickIn(dialogueAnalyzer.getCloseButtonPoint());
        mouseAloofWindow(closeButtonPoint);
        targetWindow.execute(WindowOperation.delay(100));
        targetWindow.flushScreenshot();
        close();
    }

    @Override
    public void reSetWindow() {

    }

    @Override
    public void mouseAloofWindow(Rectangle rect) {
        if (dialogueAnalyzer.mouseInRect(rect)) {
            Point safePoint = dialogueAnalyzer.findLargestRemainingAreaCenter(rect);
            targetWindow.moveTo(safePoint.x, safePoint.y);
            targetWindow.execute(WindowOperation.delay(100));
            targetWindow.flushScreenshot();
            analysis();
        }
    }
}
